﻿using UnityEngine;

namespace Room
{
    class MonsterAIStatePursue : MonsterAIStateBase
    {

        protected override void OnEnter()
        {
            var move = controller.owner.move as RCMoveMonster;
            if(move!=null)
            {
                move.SetSpeed(controller.pursueSpeed);
            }

            controller.owner.PlayAnim("runfight");
        }

        protected override void OnLeave()
        {
            var move = controller.owner.move as RCMoveMonster;
            if (move != null)
            {
                move.SetSpeed(controller.wanderSpeed);
            }
        }

        protected override void OnTick()
        {
            RoomCharactor self = controller.owner.charactorManager.GetSelfCharactor();
            Vector3 target = self.transform.position;
            Vector3 current = controller.owner.transform.position;

            if(Mathf.Abs(current.x - target.x) < 1f &&
                Mathf.Abs(current.z - target.z) < 1f)
            {
                controller.SwitchState(MonsterAIState.Hold);
                return;
            }

            var disance = Vector3.Distance(controller.owner.transform.position, controller.bornPosition);
            if (disance > controller.pursueRadius)
            {
                controller.SwitchState(MonsterAIState.BackBorn);
                return;
            }

            controller.owner.MoveTo(target);
        }
    }
}

 